package com.jiuyou.clientController.storeclassify;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jiuyou.clientController.base.ClientBaseController;
import com.jiuyou.clientController.technicianopus.TechnicianOpusClientController;
import com.ruoyi.collect.domain.Collect;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.DicConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.storeclassify.domain.Storeclassify;
import com.ruoyi.storeclassify.service.IStoreclassifyService;
import com.ruoyi.technicianOpus.domain.StoreTechnicianOpus;
import com.ruoyi.technicianOpus.service.IStoreTechnicianOpusService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *技师相关
 *
 * @author jiuyou
 */
@Api(tags = {"C端分类"})
@RestController
@RequestMapping("/client/storeClassify")
public class StoreClassifyClientController extends ClientBaseController
{
    private static final Logger log = LoggerFactory.getLogger(StoreClassifyClientController.class);
    @Autowired
    private IStoreclassifyService storeclassifyService;

    /**
     * 查询form表单中的分类
     * */
    @GetMapping("/second")
    public AjaxResult getSecond(){
        return AjaxResult.success(storeclassifyService.selectSecond());
    }
    /**
     * 查询分类列表
     */
    @ApiOperation(value = "获取分类包含子集")
    @PostMapping("/listAll")
    public AjaxResult listAll()
    {
        Storeclassify storeclassifyParam=new Storeclassify();
        storeclassifyParam.setState(DicConstants.SYS_Y.getCode());
//        storeclassifyParam.setShowWechat(DicConstants.SYS_Y.getCode());
        List<Storeclassify> list = storeclassifyService.selectStoreclassifyList(storeclassifyParam);
        Map<Long,JSONObject> levelOneMap=new HashMap<Long,JSONObject>();
        for (Storeclassify storeclassify:list) {
            JSONObject jso=new JSONObject();
            jso.put("classifyName",storeclassify.getClassifyName());
            jso.put("classifyImg",storeclassify.getClassifyImg());
            jso.put("id",storeclassify.getId());
            Long parentId= storeclassify.getParentId();
           if(null==parentId||parentId==0){
               //一级
               jso.put("child",new JSONArray());
               levelOneMap.put(storeclassify.getId(),jso);
           }else{
               JSONArray jsoArry=levelOneMap.get(parentId).getJSONArray("child");
               jsoArry.add(jso);
           }
        }
        return AjaxResult.success(levelOneMap.values());
    }
    /**
     * 查询分类列表
     */
    @ApiOperation(value = "获取一级分类")
    @PostMapping("/list")
    public AjaxResult list()
    {
        Storeclassify storeclassifyParam=new Storeclassify();
        storeclassifyParam.setState(DicConstants.SYS_Y.getCode());
        storeclassifyParam.setShowWechat(DicConstants.SYS_Y.getCode());
        storeclassifyParam.setParentId(0L);
        List<Storeclassify> list = storeclassifyService.selectStoreclassifyList(storeclassifyParam);
        Map<Long,JSONObject> levelOneMap=new HashMap<Long,JSONObject>();
        for (Storeclassify storeclassify:list) {
            JSONObject jso=new JSONObject();
            jso.put("classifyName",storeclassify.getClassifyName());
            jso.put("id",storeclassify.getId());
            levelOneMap.put(storeclassify.getId(),jso);
        }
        return AjaxResult.success(levelOneMap.values());
    }
    /**
     * 查询分类列表
     */
    @ApiOperation(value = "获取一级分类")
    @PostMapping("/listSecond/{id}")
    public AjaxResult listSecond(@PathVariable Long id)
    {
        Storeclassify storeclassifyParam=new Storeclassify();
        storeclassifyParam.setState(DicConstants.SYS_Y.getCode());
        storeclassifyParam.setShowWechat(DicConstants.SYS_Y.getCode());
        storeclassifyParam.setParentId(id);
        List<Storeclassify> list = storeclassifyService.selectStoreclassifyList(storeclassifyParam);
        Map<Long,JSONObject> levelOneMap=new HashMap<Long,JSONObject>();
        for (Storeclassify storeclassify:list) {
            JSONObject jso=new JSONObject();
            jso.put("classifyName",storeclassify.getClassifyName());
            jso.put("id",storeclassify.getId());
            levelOneMap.put(storeclassify.getId(),jso);
        }
        return AjaxResult.success(levelOneMap.values());
    }
    /**
     * 查询分类列表
     */
    @ApiOperation(value = "首页分类包含子")
    @PostMapping("/customerList")
    public AjaxResult customerList()
    {
        ArrayList<Long> parentIds = new ArrayList<>(4);
        parentIds.add(1L);
        parentIds.add(2L);
        parentIds.add(3L);
        parentIds.add(4L);
        List<Storeclassify> list = storeclassifyService.selectStoreclassifyCustomerList(parentIds);
        List<Storeclassify> childList = storeclassifyService.selectStoreclassifyCustomerListChild(parentIds);
        list.addAll(childList);
//        ArrayList<Long> childIds = new ArrayList<>();
//        list.forEach(e-> {
//            childIds.add(e.getParentId());
//        });
        Map<Long,JSONObject> levelOneMap=new HashMap<Long,JSONObject>();
        for (Storeclassify storeclassify:list) {
            JSONObject jso=new JSONObject();
            jso.put("classifyName",storeclassify.getClassifyName());
            jso.put("classifyImg",storeclassify.getClassifyImg());
            jso.put("id",storeclassify.getId());
            Long parentId= storeclassify.getParentId();
            if(null==parentId||parentId==0){
                //一级
                jso.put("child",new JSONArray());
                levelOneMap.put(storeclassify.getId(),jso);
            }else{
                JSONObject jsoOne=levelOneMap.get(parentId);
                if(null!=jsoOne){
                    JSONArray jsoArry=jsoOne.getJSONArray("child");
                    jsoArry.add(jso);
                }
            }
        }
        return AjaxResult.success(levelOneMap.values());
    }


}
